
import axios from "axios"

export default {
    namespaced:true,
    state:{
        firstView:true,
        loading:false,
        users:[],
        errorMsg:''
    },
    mutations:{
        requesting(state){
            state.firstView=false
            state.loading = true
        },
        reqsuccess(state,users){
            state.users = users
            state.loading = false
        },
        reqError(state,errorMsg){
            state.loading = false
            state.errorMsg = errorMsg
        }

    },
    actions:{
        async seatch({commit},title){
            commit('requesting')

            try{  
                const res = await axios.get(`/dev-api/search/users?q=${title}`)
                const users = res.data.items.map((item)=>({
                  name:item.login,
                  url:item.html_url,
                  avatar_url:item.avatar_url
                }))

                commit('reqsuccess',users)

                }catch(error){

                  commit('reqError','请求错误'+error.message)

                }
        }
    },
    gettes:{

    }
}